import { _decorator, BoxCollider2D, CircleCollider2D, Component, EventTouch, Input, instantiate, Node, PolygonCollider2D, Prefab, RigidBody2D, tween, UITransform, v3, Vec2, Vec3 } from 'cc';
import { LayerAction } from './LayerAction';
import { ElementAction } from './ElementAction';
import { PinAction } from './PinAction';

import { LevelAction } from './LevelAction';
import { Tools } from './Tools';
import { Global } from './Global';
import AssetMgr from '../frame/AssetMgr';
const { ccclass, property } = _decorator;

@ccclass('LayerRootAction')
export class LayerRootAction extends Component {
    lvl_action:LevelAction
    
    

    public async init_root(lvl: number) {
        //lvl=7
        Tools.clearFromParent(this.node, LevelAction);
        let level_prebab = await AssetMgr.Inst.getOnePrefab("prefabs/game/lvl_" + lvl , "gameBundle");
        console.log(level_prebab)
        if (!level_prebab) {
            console.log("没有这个关卡》〉》〉》", lvl);
            //return
            console.log("重置一个等级1");
            Global.restart_default(0);
            lvl = Global.next_level();
            level_prebab = await AssetMgr.Inst.getOnePrefab("prefabs/game/lvl_" + lvl , "gameBundle");
        }
        let lvl_node = instantiate(level_prebab);
        this.lvl_action = lvl_node.getComponent(LevelAction);
        this.node.addChild(lvl_node);
        this.lvl_action.init_level();
    }

    /** 根据json 生成每层板子 合并关卡 */
    public async init_root_read_config(json:any){
        console.log("读取json 生成关卡",json)
        
        Tools.clearFromParent(this.node, LevelAction);
        let level_prebab = await AssetMgr.Inst.getOnePrefab("prefabs/game/lvl_json", "gameBundle");
        console.log(level_prebab)
        
        let lvl_node = instantiate(level_prebab);
        
        let _data:[] = json.layer;
        //反转数组
        const __data = _data.slice().reverse()

        for (let i = 0; i < __data.length; i++) {
            const _layer = __data[i];
            const _layer_num = Math.abs(_layer).toString().length;
            let _layer_str = ""+_layer;
            if(_layer_num == 1)_layer_str = "00"+_layer;
            else if(_layer_num == 2)_layer_str = "0"+_layer;
            // else if(_layer_num == 3)_layer_str = ""+_layer;

            // console.log("几位数：(需要判断前面是否加0)",_layer_str)
            let layer_prebab = await AssetMgr.Inst.getOnePrefab("layer/layer_" + _layer_str , "gameBundle");
            let layer_node = instantiate(layer_prebab);
            
            lvl_node.addChild(layer_node)//将板子加到本关子节点

        }

        this.lvl_action = lvl_node.getComponent(LevelAction);
        this.node.addChild(lvl_node);

        
    }

    /** 根据json 生成电池颜色 */
    public init_color_data(bat_data:number[] , bat_c_data:number[]){
        this.lvl_action.init_level_config(bat_data , bat_c_data);
    }

    showRoot(){
        this.node.setPosition(v3(0,-288,0));
    }

    random_pin() {
        let arr: PinAction[] = [];
        if(this.lvl_action) {
           arr =  this.lvl_action.get_pin_color();
        }

        let t = 0.05;
        let y = this.node.getPosition().y;
        tween(this.node)
            .to(t, { position: new Vec3(30, y,0) },{ easing: 'quadInOut' })
            .to(t, { position: new Vec3(-30, y,0) },{ easing: 'quadInOut' })
            .to(t, { position: new Vec3(30, y,0) },{ easing: 'quadInOut' })
            .to(t, { position: new Vec3(-30, y,0) },{ easing: 'quadInOut' })
            .to(t, { position: new Vec3(20, y,0) },{ easing: 'quadInOut' })
            .to(t, { position: new Vec3(-20, y,0) }, { easing: 'quadInOut' })
            .to(t, { position: new Vec3(20, y,0) }, { easing: 'quadInOut' })
            .to(t, { position: new Vec3(-20, y,0) },{ easing: 'quadInOut' })
            .to(t, { position: new Vec3(10, y,0) },{ easing: 'quadInOut' })
            .to(t, { position: new Vec3(-10, y,0) },{ easing: 'quadInOut' })
            .to(t, { position: new Vec3(2, y,0) },{ easing: 'quadInOut' })
            .to(t, { position: new Vec3(-2, y,0) },{ easing: 'quadInOut' })
            .to(t, { position: new Vec3(0, y,0) },{ easing: 'quadInOut' })
            .delay(0.3)
            .call(() => {
                this.node.setPosition(new Vec3(0, y,0));

                let arr_color = [];
                arr.forEach(element => {
                    let c = element.pin_color;
                    arr_color.push(c.clone());
                });
                arr_color.sort(() => {
                    return 0.5 - Math.random();
                });
                if (arr.length == arr_color.length) {
                    for (let i = 0; i < arr.length; i++) {
                        let pin_color = arr[i].pin_color;
                        let c = arr_color[i];
                        pin_color.r = c.r;
                        pin_color.g = c.g;
                        pin_color.b = c.b;
                        pin_color.id = c.id;
                        arr[i].reset_img();
                    }
                }
            })
            .start();


    }

}

